package com.example.demo.service.impl;

import com.example.demo.pojo.ChatGroupPO;
import com.example.demo.service.ChatGroupService;
import com.example.demo.utility.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
/**
 * ChatGroupService实现类
 */
public class ChatGroupServiceImpl implements ChatGroupService {
    RedisUtil redisUtil = new RedisUtil();
    //接收到key值和密码后，存入redis，并返回结果
    public ChatGroupPO setChatGroupName(String key, String name) {

        try {
            redisUtil.set(key, name,86400);
            //将群聊信息返回
            ChatGroupPO chatGroupPO = new ChatGroupPO();
            chatGroupPO.setKey(key);
            chatGroupPO.setName(name);
            return chatGroupPO;
        } catch (Exception e) {
            e.printStackTrace();
            // 设置失败时返回失败
            return null;
        } finally {
            redisUtil.close();
        }
    }

    @Override
    public ArrayList<ChatGroupPO> getChatGroup(String key) {
        try {
            ArrayList<ChatGroupPO> keyByPrefix = redisUtil.getKeyByPrefix(key);
            return keyByPrefix;
        } catch (Exception e) {
            e.printStackTrace();
            // 设置失败时返回失败
            return null;
        } finally {
//            redisUtil.close();
        }
    }
}
